package com.lghs.vickual_pf;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Activity_Stats extends Activity {

	// Global calls
	static EditText
	/* Base EditTexts */
	BaseSTR, BaseDEX, BaseCON, BaseINT, BaseWIS, BaseCHA,
	/* Mod EditTexts */
	STRmod, DEXmod, CONmod, INTmod, WISmod, CHAmod,
	/* Race EditTexts */
	STRrace, DEXrace, CONrace, INTrace, WISrace, CHArace;
	/* Numbers */
	static int
	STRmodded, DEXmodded, CONmodded, INTmodded, WISmodded, CHAmodded;
	/* Toast (Why well why not?) */
	static Toast msg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stats);
		setUpView();
	}

	private void setUpView() {
		/* Base Link */
		BaseSTR = (EditText) findViewById(R.id.stats_editText_BaseSTR);
		BaseDEX = (EditText) findViewById(R.id.stats_EditText_BaseDEX);
		BaseCON = (EditText) findViewById(R.id.stats_EditText_BaseCON);
		BaseINT = (EditText) findViewById(R.id.stats_EditText_BaseINT);
		BaseWIS = (EditText) findViewById(R.id.stats_EditText_BaseWIS);
		BaseCHA = (EditText) findViewById(R.id.stats_EditText_BaseCHA);
		// http://www.d20pfsrd.com/basics-ability-scores/character-creation
		/* Mod link */
		STRmod = (EditText) findViewById(R.id.stats_editText_STRmod);
		DEXmod = (EditText) findViewById(R.id.stats_EditText_DEXmod);
		CONmod = (EditText) findViewById(R.id.stats_EditText_CONmod);
		INTmod = (EditText) findViewById(R.id.stats_EditText_INTmod);
		WISmod = (EditText) findViewById(R.id.stats_EditText_WISmod);
		CHAmod = (EditText) findViewById(R.id.stats_EditText_CHAmod);
		/* Race Link */
		STRrace = (EditText) findViewById(R.id.stats_EditText_raceSTR);
		DEXrace = (EditText) findViewById(R.id.stats_EditText_raceDEX);
		CONrace = (EditText) findViewById(R.id.stats_EditText_raceCON);
		INTrace = (EditText) findViewById(R.id.stats_EditText_raceINT);
		WISrace = (EditText) findViewById(R.id.stats_EditText_raceWIS);
		CHArace = (EditText) findViewById(R.id.stats_EditText_raceCHA);
	}

	public int getmodfrom(int base) {
		int mod = 18;
		if (base <= 1)
			mod = -5;
		else if (base <= 3)
			mod = -4;
		else if (base <= 5)
			mod = -3;
		else if (base <= 7)
			mod = -2;
		else if (base <= 9)
			mod = -1;
		else if (base <= 11)
			mod = 0;
		else if (base <= 13)
			mod = 1;
		else if (base <= 15)
			mod = 2;
		else if (base <= 17)
			mod = 3;
		else if (base <= 19)
			mod = 4;
		else if (base <= 21)
			mod = 5;
		else if (base <= 23)
			mod = 6;
		else if (base <= 25)
			mod = 7;
		else if (base <= 27)
			mod = 8;
		else if (base <= 29)
			mod = 9;
		else if (base <= 31)
			mod = 10;
		else if (base <= 33)
			mod = 11;
		else if (base <= 35)
			mod = 12;
		else if (base <= 37)
			mod = 13;
		else if (base <= 39)
			mod = 14;
		else if (base <= 41)
			mod = 15;
		else if (base <= 43)
			mod = 16;
		else if (base <= 45)
			mod = 17;
		return mod;
	}

	public void calcMods(View v) {
		new setstats().execute();
	}

	class setstats extends AsyncTask<String, String, Void> {

		@Override
		protected Void doInBackground(String... params) {
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			try {
				/* Get mods */
				STRmodded = getmodfrom(Integer.parseInt(BaseSTR.getText()
						.toString()));
				DEXmodded = getmodfrom(Integer.parseInt(BaseDEX.getText()
						.toString()));
				CONmodded = getmodfrom(Integer.parseInt(BaseCON.getText()
						.toString()));
				INTmodded = getmodfrom(Integer.parseInt(BaseINT.getText()
						.toString()));
				WISmodded = getmodfrom(Integer.parseInt(BaseWIS.getText()
						.toString()));
				CHAmodded = getmodfrom(Integer.parseInt(BaseCHA.getText()
						.toString()));

				/* Set Mods */
				STRmod.setText(String.valueOf(STRmodded));
				DEXmod.setText(String.valueOf(DEXmodded));
				CONmod.setText(String.valueOf(CONmodded));
				INTmod.setText(String.valueOf(INTmodded));
				WISmod.setText(String.valueOf(WISmodded));
				CHAmod.setText(String.valueOf(CHAmodded));

				/* Set Bonuses */
				Bundle bund = getIntent().getExtras();
				STRrace.setText(String.valueOf(bund.getInt("BonusSTR")));
				DEXrace.setText(String.valueOf(bund.getInt("BonusDEX")));
				CONrace.setText(String.valueOf(bund.getInt("BonusCON")));
				INTrace.setText(String.valueOf(bund.getInt("BonusINT")));
				WISrace.setText(String.valueOf(bund.getInt("BonusWIS")));
				CHArace.setText(String.valueOf(bund.getInt("BonusCHA")));
			} catch (Exception x) {
				if (x.getMessage().contains("Invalid int: \"\"")) {
					msg = Toast.makeText(getApplicationContext(),
							"Recheck your stats", Toast.LENGTH_LONG);
					msg.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0,
							0);
					if (msg.getView().getWindowVisibility() != View.VISIBLE) {
						msg.show();
					}
				}
			}
		}
	}
}
